package com.nuc.thread.reentrantlock;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description ReentrantLock 可打断
 * @createTime 2021年07月10日 10:57:00
 */
@Slf4j
public class Test2 {
    private static ReentrantLock lock = new ReentrantLock();
    @SneakyThrows
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            log.debug("尝试获取锁");
            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("被打断.没有获取到锁");
                return;
            }
            try {
                log.debug("获取到锁");
            } finally {
                lock.unlock();
            }
        }, "t1");

        lock.lock();
        t1.start();

        Thread.sleep(1000);
        log.debug("打断锁");
        t1.interrupt();


    }
}
